<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="/static/common/taglib.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>发布通知</title>
    <%@include file="../../public/commonCss.jsp" %>
    <%@include file="../../public/commonJs.jsp" %>
    <script type="text/javascript" src="${ctx}/static/js/ajaxfileupload.js"></script>
</head>
<body class="ms-controller" ms-controller="main">
<div id="wrapper">
    <div class="boxmain whiteBox lrbox">
        <div class="lrboxmain borderL" style="margin-left:0%">
            <div class="from-header borderB contentbox">
                <div>
                    <span class="font14"> 所属分类:</span><a id="category"><span class="fontOrange2">发布公告</span></a>
                    <button class="button blueButton" style="float:right;margin-right:20px;margin-left: 10px"
                            onclick="ajaxFileUpload()">上传
                    </button>
                    <select style="float:right;margin-left: 10px" ms-duplex="category">
                        <option value="">---请选择附件分类---</option>
                        <option ms-attr-value="el.itemValue" ms-repeat="categories">{{el.itemText}}</option>
                    </select>
                    <input type="file" name="file" id="file" style="float:right;margin-top: 4px;"/>
                </div>
            </div>
            <div class="from-body withheader contentbox" style=" overflow: auto;">
                <table style="width:100%;" cellpadding="5" cellspacing="0" border="0" class="tableStyle">
                    <thead>
                    <tr align="center">
                        <th>行号</th>
                        <th>上传人</th>
                        <th align="left">附件名称</th>
                        <th>上传时间</th>
                        <th>附件类型</th>
                        <th>所属分类</th>
                        <th>删除</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr align="center" ms-repeat="tableDatas">
                        <td>{{$index+1}}</td>
                        <td>{{el.uploaderName}}</td>
                        <td align="left"
                            ms-if="el.fileType=='png'||el.fileType=='PNG'||el.fileType=='jpg'||el.fileType=='JPG'">
                            <a href="javascript:;" ms-on-click="showLayer(el.filePath)" style="color:#4287cf;">{{el.fileName}}</a>
                        </td>
                        <td align="left"
                            ms-if="!(el.fileType=='png'||el.fileType=='PNG'||el.fileType=='jpg'||el.fileType=='JPG')">
                            <a ms-attr-href="${fileServerUrl}{{el.filePath}}" style="color:#4287cf;">{{el.fileName}}</a>
                        </td>
                        <td>{{el.uploadTime}}</td>
                        <td>{{el.fileType}}</td>
                        <td>{{el.categoryName}}</td>
                        <td><a href="javascript:;" class="operate del" title="删除" ms-on-click="deleteData(el)"></a></td>
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <!-- whitebox -->
</div>
<script type="text/javascript">
    var vm;
    avalon.ready(function () {
        vm = avalon.define({
            $id: "main",
            tableDatas: [],
            categories: [],
            category: ''
        });
        avalon.scan();
        loadTableData();
        loadCategoryData();
    });

    //异步上传文件
    function ajaxFileUpload() {
        var fileName = $("#file").val();
        if (fileName == null || fileName == '') {
            top.layer.alert('没有选择文件，请先选择文件.');
            return;
        }
        if (vm.$model.category == "") {
            top.layer.alert('请选择附件分类');
            return;
        }
        $.ajaxFileUpload({
            url: '${ctx}/noticeFileController/uploadFile.action?category=' + vm.$model.category,//用于文件上传的服务器端请求地址
            secureuri: false, //是否需要安全协议，一般设置为false
            fileElementId: 'file', //文件上传域的ID
            dataType: 'text', //返回值类型 一般设置为json
            success: function (data, status) {
                var dataJson = eval('(' + $(data).text() + ')');
                closeDialog();
                if (dataJson.code == 0) {
                    top.layer.msg('文件上传成功', {icon: 1});
                    loadTableData();
                } else if (dataJson.code == -1) {
                    top.layer.msg('文件上传失败', {icon: 2});
                } else if (dataJson.code == -2) {
                    top.layer.alert('登录超时，请重新登录', function () {
                        window.href.location = '${ctx}/logout.action'
                    });
                } else if (dataJson.code == -3) {
                    top.layer.msg('文件不能为空', {icon: 2});
                } else if (dataJson.code == -4) {
                    top.layer.msg('数据保存失败', {icon: 2});
                }

            }
        })
    }
    function loadCategoryData() {
        $.ajax({
            type: 'post',
            url: '${ctx}/noticeFileController/selectAllCategories.action',
            data: '',
            dataType: "json",
            timeout: 10000,
            success: function (data) {
                if (data.code == 0) {
                    vm.categories = data.datas;
                }
            }
        });
    }
    //加载表格数据
    function loadTableData() {
        $.ajax({
            type: 'post',
            url: '${ctx}/noticeFileController/selectAllNoticeFile.action',
            data: '',
            dataType: "json",
            timeout: 10000,
            beforeSend: function () {
                layui.layer.msg('正在加载数据...', {
                    icon: 16,
                    shade: 0.01
                });
            },
            success: function (data) {
                closeDialog();
                if (data.code == 0) {
                    vm.tableDatas = data.datas;
                } else {
                    layui.layer.msg('数据加载失败...');
                }
            }
        });
    }
    //删除
    function deleteData(el) {
        layui.layer.alert('你确定删除该记录吗?', {
            title: '提醒',
            btn: ['确定', '取消']
        }, function () {
            //执行删除的ajax
            $.ajax({
                type: 'post',
                url: '${ctx}/noticeFileController/deleteNoticeFile.action?id=' + el.id,
                data: "",
                dataType: "json",
                timeout: 10000,
                success: function (data) {
                    if (data.code == 0) {
                        layer.msg('删除成功', {icon: 1});
                        loadTableData();
                    } else if (data.code == -1) {
                        layer.msg('删除失败,请重试', {icon: 2});
                    }

                }
            });
        });
    }
    //显示layer
    function showLayer(imagePath) {
        parent.layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            skin: 'layui-layer-nobg', //没有背景色
            shadeClose: true,
            content: "<img src='${fileServerUrl}" + imagePath + "'/>"
        });
    }
</script>
</body>
</html>